package com.budwk.app.sqfwV2.service.weiqingmen;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.GenerateWordAndPdfUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.SafetyCheckDTO;
import com.budwk.app.sqfwV2.enums.weiqingmen.SafetyCheckEnum;
import com.budwk.app.sqfwV2.generator.weiqingmen.CateringServiceLastPdfGenerator;
import com.budwk.app.sqfwV2.generator.weiqingmen.CateringServicePdfGenerator;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.google.common.collect.Lists;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 餐饮经营服务场所消防安全巡查台账pdf
 * @BelongsProject: dfxy-shequ
 * @Author: yangweimin
 * @CreateTime: 2024-04-02  11:24
 * @Description: TODO
 */
@IocBean
public class FireSafetyInCateringEstablishmentsPdfService extends BasePdfService {
    @Inject
    SysUserService sysUserService;
    @Inject
    SqfwFormsummaryNewService sqfwFormsummaryNewService;

    /**
     * 创建pdf
     * @param paramForController
     */
    public void createPdf(CompleteFlowPdfParamForController paramForController) {
        super.createPdf(SafetyCheckEnum.FIRE_SAFETY_IN_CATERING_ESTABLISHMENTS, paramForController);
    }

    @Override
    protected AbstractCustomerPdfCreateField getCustomerPdfField(String jasonStr) {
        return JSONObject.parseObject(jasonStr, SafetyCheckDTO.class);
    }

    @Override
    protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam) {
        SafetyCheckDTO dto = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        Cnd cnd = Cnd.NEW();
        cnd.and("param_data->>'attributesOfInspectionUnits'", "=", dto.getAttributesOfInspectionUnits());
        cnd.and("param_data->>'placeName'", "=", dto.getPlaceName());
        cnd.and("param_data->>'address'", "=", dto.getAddress());
        cnd.and("param_data->>'inspectionDate'", "<", dto.getInspectionDate().getTime());
        cnd.orderBy("param_data->>'inspectionDate'", "desc");
        cnd.limit(1);
        Sqfw_formSummary_new lastFormSummary = sqfwFormsummaryNewService.fetch(cnd);
        Map<String, Object> customerParamMap = new HashMap<>();
        customerParamMap.put("lastFormSummary", lastFormSummary);
        if (lastFormSummary != null) {
            Sys_user user = sysUserService.fetch(lastFormSummary.getCreateBy());
            customerParamMap.put("createUserName", user.getUsername());
            customerParamMap.put("lastSignatureUrl", user.getSignatureUrl());
        }
        pdfGenerateParam.setCustomerParamMap(customerParamMap);
    }

    @Override
    protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam) {
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();
        List<BasePdfGenerator> generatorList = Lists.newArrayList();

        // 餐饮经营服务场所消防安全巡查台账(上一次)
        generatorList.add(new CateringServiceLastPdfGenerator());

        // 餐饮经营服务场所消防安全巡查台账(本次)
        generatorList.add(new CateringServicePdfGenerator());

        return generatorList;
    }

    /**
     * 下载word模板
     * @param config
     * @return
     */
    public String downWordTemplate(ExcelCreateConfig config) {
        String fileName = SafetyCheckEnum.getTemplateName(config.getFileIdentity());

        List<NiceXWPFDocument> xwpfDocumentList = new ArrayList<>();

        SafetyCheckPdfService safetyCheckPdfService = NutzIocUtils.getBean(SafetyCheckPdfService.class);

        // 餐饮经营服务场所消防安全巡查台账
        xwpfDocumentList.add(safetyCheckPdfService.getNiceXWPFDocument(new CateringServicePdfGenerator().getTemplatePath(null)));

        GenerateWordAndPdfUrlVO vo = CommonFormOfficeUtils.wordTemplateUpload(xwpfDocumentList, fileName);

        return vo.getWordUrl();
    }

    @Override
    protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam) {
        return SafetyCheckEnum.FIRE_SAFETY_IN_CATERING_ESTABLISHMENTS.getCompilationName();
    }

    @Override
    protected String getSummaryType() {
        return null;
    }
}
